package com.agu.mgt.code.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;

import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.entity.code.CodColumnEntity;
import com.agu.mgt.code.service.CodColumnService;
import com.agu.utils.Query;
import com.github.pagehelper.Page;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.FormatUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 代码表
 * 
 * @author 
 * @email 
 * @date 2019-01-11 16:14:55
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("代码表")
@RequestMapping("codcolumn")
public class CodColumnController extends AbstractController{
	@Autowired
	private CodColumnService codColumnService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("codcolumn:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		Query query = new Query(params);
		Page<CodColumnEntity> page = codColumnService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info/{columnId}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("codcolumn:info")
	public Result info(@PathVariable("columnId") Long columnId){
		CodColumnEntity codColumn = codColumnService.queryObject(columnId);
		
		return ResultGenerator.successData(codColumn);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增代码表")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("codcolumn:save")
	public Result save(@RequestBody CodColumnEntity codColumn){
	
		codColumnService.save(codColumn);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("columnId","字段ID");
		     cmap.put("columnName","字段名");
		     cmap.put("columnType","字段类别");
		     cmap.put("columnLength","字段长度");
		     cmap.put("decimalLength","小数长度");
		     cmap.put("comments","备注");
		     cmap.put("isPkey","是否主键");
		     cmap.put("notNull","不能为空");
		     cmap.put("noList","不在列表中");
		     cmap.put("noInput","不在输入中");
		     cmap.put("noView","不在详情中");
		     cmap.put("createTime","时间");
		String remark = CompareUtils.getObjectRemark(codColumn, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改代码表")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("codcolumn:update")
	public Result update(@RequestBody CodColumnEntity codColumn){
	
		CodColumnEntity codColumn1 = codColumnService.queryObject(codColumn.getColumnId());
	
		codColumnService.update(codColumn);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		
		     cmap.put("columnId","字段ID");
		     cmap.put("columnName","字段名");
		     cmap.put("columnType","字段类别");
		     cmap.put("columnLength","字段长度");
		     cmap.put("decimalLength","小数长度");
		     cmap.put("comments","备注");
		     cmap.put("isPkey","是否主键");
		     cmap.put("notNull","不能为空");
		     cmap.put("noList","不在列表中");
		     cmap.put("noInput","不在输入中");
		     cmap.put("noView","不在详情中");
		     cmap.put("createTime","时间");
	
		String remark = CompareUtils.getTwoObjectRemark(codColumn1, codColumn, cmap);
	
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除代码表")
	@GetMapping("/delete/{columnId}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("codcolumn:delete")
	public Result delete(@PathVariable("columnId") Long columnId){
	
	    	if (columnId == null) {
			return ResultGenerator.fail("缺少参数");
		}
	
		CodColumnEntity codColumn = codColumnService.queryObject(columnId);
	
		codColumnService.delete(columnId);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("columnId","字段ID");
		     cmap.put("columnName","字段名");
		     cmap.put("columnType","字段类别");
		     cmap.put("columnLength","字段长度");
		     cmap.put("decimalLength","小数长度");
		     cmap.put("comments","备注");
		     cmap.put("isPkey","是否主键");
		     cmap.put("notNull","不能为空");
		     cmap.put("noList","不在列表中");
		     cmap.put("noInput","不在输入中");
		     cmap.put("noView","不在详情中");
		     cmap.put("createTime","时间");
		String remark = CompareUtils.getObjectRemark(codColumn, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}
	
	/**
	 * 批量删除
	 */
	@SysLog("批量删除代码表")
	@PostMapping("/deletes")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("codcolumn:delete")
	public Result deletes(@RequestBody Long[] columnIds){
	
		codColumnService.deleteBatch(columnIds);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("codcolumn:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<CodColumnEntity> list = codColumnService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

               Map<String, String> columnTypeMap = new HashMap<String, String>();

                columnTypeMap.put("Integer", "整型");
                columnTypeMap.put("Float", "浮点数");
                columnTypeMap.put("Long", "长整型");
                columnTypeMap.put("String", "字符串");
                columnTypeMap.put("Double", "双浮点数");
                columnTypeMap.put("Date", "日期");

            map.put("columnTypeMap", columnTypeMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("字段ID", "columnId", 1);
			clist.add(cloumn);
            cloumn = new Column("字段名", "columnName", 1);
			clist.add(cloumn);
            cloumn = new Column("字段类别", "columnType", 2);
			clist.add(cloumn);
            cloumn = new Column("字段长度", "columnLength", 1);
			clist.add(cloumn);
            cloumn = new Column("小数长度", "decimalLength", 1);
			clist.add(cloumn);
            cloumn = new Column("备注", "comments", 1);
			clist.add(cloumn);
            cloumn = new Column("是否主键", "isPkey", 1);
			clist.add(cloumn);
            cloumn = new Column("不能为空", "notNull", 1);
			clist.add(cloumn);
            cloumn = new Column("不在列表中", "noList", 1);
			clist.add(cloumn);
            cloumn = new Column("不在输入中", "noInput", 1);
			clist.add(cloumn);
            cloumn = new Column("不在详情中", "noView", 1);
			clist.add(cloumn);
            cloumn = new Column("时间", "createTime", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
